package com.example.projectjava.controller;

import com.example.projectjava.service.LogisticService;
import com.example.projectjava.service.ProductService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name = "产品接口")
public class ProductController {
    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    //这种注解是stringboot的一个核心功能，是依赖注入功能
    JdbcTemplate  jdbcTemplate;//自动传入了这个对象

    @Autowired
    ProductService productservice;

    @ResponseBody //此注解作用：将方法返回值转换为json串返回给客户端
    @GetMapping("/productadd")//添加商品
    @Operation(summary = "添加商品")
    @Parameters({
            @Parameter(name ="product_id",description="商品id(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="category_id",description="商品分类id(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="product_name",description="商品名(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="price",description="商品价格(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="stock",description="商品库存(不允许为空)",in = ParameterIn.QUERY,required = true)

    })
        public  JsonResponse  productadd(int product_id,int category_id,String product_name,double price,
                                        int stock){
            JsonResponse jsonRespone = new JsonResponse();
        if (product_id == 0) {
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品名不能为空");
            //  flag = false;
            return  jsonRespone;
        }

        if (price<0) {
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品价格不能小于零");
            //  flag = false;
            return  jsonRespone;
        }
        if (category_id < 0) {
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品库存不能为负数");
            //  flag = false;
            return  jsonRespone;
        }
        try {
            // Sql

            productservice.productadd(product_id,category_id,product_name,price, stock);
            // jdbcTemplate.update(sql, userName,goodsName,price);
            jsonRespone.setCode(0);
            jsonRespone.setMsg("商品添加成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonRespone.setCode(1);
            jsonRespone.setMsg("商品添加失败:"+e.getMessage());
        }
        return  jsonRespone;
    }


    @ResponseBody
    @GetMapping("/deleteproduct")
    @Operation(summary = "删除商品")
    @Parameters({
            @Parameter(name ="product_id",description="商品id(不允许为空)",in = ParameterIn.QUERY,required = false)

    })
    public  JsonResponse  productdelete(int product_id){
        JsonResponse jsonRespone = new JsonResponse();
        try{
        productservice.deleteproduct(product_id);
        jsonRespone.setCode(0);
        jsonRespone.setMsg("删除成功");}
        catch (Exception e){
            e.printStackTrace();
            jsonRespone.setCode(1);
            jsonRespone.setMsg("删除失败："+e.getMessage());
        }
        return  jsonRespone;
    }


    @ResponseBody //此注解作用：将方法返回值转换为json串返回给客户端
    @GetMapping("/productupdate")//修改商品
    public  JsonResponse  productupdate(String product_id,String category_id,String product_name,String price,String stock){
        JsonResponse jsonRespone = new JsonResponse();
        try{
            productservice.productupdate(product_id,category_id,product_name,price,stock);
            jsonRespone.setCode(0);
            jsonRespone.setMsg("修改成功");}
        catch (Exception e){
            e.printStackTrace();
            jsonRespone.setCode(1);
            jsonRespone.setMsg("修改失败："+e.getMessage());
       }
        return  jsonRespone;
    }

    @ResponseBody
    @GetMapping("/queryAllProduct")
    @Operation(summary = "查询所有商品")
    public  JsonResponse queryproduct(){

        List<Map<String,Object>> result =productservice.queryproduct();

        JsonResponse jsonRespone = new JsonResponse();
        jsonRespone.setCode(0);
        jsonRespone.setMsg("查询成功");
        jsonRespone.setData(result);
        return  jsonRespone;
    }
}
